package com.cortex.carambolasummary;

import java.util.ArrayList;
import java.util.List;

import com.cortex.carambola.database.DataBase;
import com.cortex.carambola.fetch.FetchSummary;
import com.cortex.carambola.fetch.FetchSummary.OnStateChanged;
import com.cortex.carambola.summary.CarambolaRequestSummary;
import com.cortex.carambola.summary.CarambolaSummary;

import android.app.Application;

public class CarambolaApplication extends Application implements OnStateChanged{

	private FetchSummary fetchSummary;
	private OnStateSummaryChanged onStateSummaryChanged;

	private boolean isOnlineRequestSummary;
	private CortexList<CarambolaSummary> dataBaseSummaries,seasonSummaries;

	public CarambolaApplication() {
		fetchSummary = new FetchSummary();
		fetchSummary.setOnStateChanged(this);
		seasonSummaries = new CarambolaSeasonSummaries();
		dataBaseSummaries = new CarambolaDataBaseSummaries();
	}
	public void setOnStateSummaryChanged(
			OnStateSummaryChanged onStateSummaryChanged) {
		this.onStateSummaryChanged = onStateSummaryChanged;
	}
	public void runSummary(CarambolaRequestSummary requsetSummary,boolean isOnlineRequestSummary){
		this.isOnlineRequestSummary = isOnlineRequestSummary;
		fetchSummary.startFetching(requsetSummary);
	}
	public OnActionList getDataBaseSummaries(){
		return dataBaseSummaries;
	}
	public OnActionList getSeasonSummaries(){
		return seasonSummaries;
	}

	@Override
	public void timeLoadStarted() {
		if(onStateSummaryChanged != null && isOnlineRequestSummary){
			onStateSummaryChanged.summaryTimeLoadStarted();
		}
	}

	@Override
	public void timeLoadFinished(CarambolaSummary summary) {
		if(summary.isSummarySuccess()){
			dataBaseSummaries.add(summary);
			if(!isOnlineRequestSummary){
				seasonSummaries.add(summary);
			}
		}
		if(onStateSummaryChanged != null ){
			onStateSummaryChanged.summaryTimeLoadFinished(summary,isOnlineRequestSummary);
		}
	}

	@Override
	public void timeLoadCanceled(String message) {
		if(onStateSummaryChanged != null){
			onStateSummaryChanged.summaryTimeLoadCanceled(message);
		}
	}
	public interface OnStateSummaryChanged{
		void summaryTimeLoadStarted();
		void summaryTimeLoadFinished(CarambolaSummary summary,boolean isOnlineRequestSummary);
		void summaryTimeLoadCanceled(String message);
	}
	public List<CarambolaSummary> getDataBase(){
		DataBase db = new DataBase(this);
		return db.readAllData();
	}
	public class CarambolaSeasonSummaries extends CortexList<CarambolaSummary>{
		private List<CarambolaSummary> list;
		@Override
		void summaryAdded(CarambolaSummary summary) {

		}
		@Override
		void summaryRemoved(CarambolaSummary summary) {

		}
		@Override
		void summariesCleared(List<CarambolaSummary> summaries) {

		}
		@Override
		List<CarambolaSummary> getList() {
			if(list == null){
				list = new ArrayList<CarambolaSummary>();
			}
			return list;
		}

	}
	public class CarambolaDataBaseSummaries extends CortexList<CarambolaSummary>{
		private DataBase db;
		private List<CarambolaSummary> list;
		@Override
		void summaryAdded(CarambolaSummary summary) {
			getDataBase().insert(summary);
		}
		@Override
		void summaryRemoved(CarambolaSummary summary) {
			getDataBase().delete(summary);
		}
		@Override
		void summariesCleared(List<CarambolaSummary> summaries) {
			getDataBase().deleteAll();
		}
		private DataBase getDataBase(){
			if(db == null){
				db = new DataBase(CarambolaApplication.this);
			}
			return db;
		}
		@Override
		List<CarambolaSummary> getList() {
			if(list == null){
				list = getDataBase().readAllData();
			}
			return list;
		}
	}
}
